package com.open.party.cloud.view.redMeeting

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import cn.android.x.model.data.AppPageData
import cn.android.x.model.data.ResultInfo
import cn.android.x.parent.AppEventBusTitleBaseActivity
import com.open.party.cloud.R
import com.open.party.cloud.model.ArticleEntity
import com.open.party.cloud.view.home.article.fragment.ArticleListFragment
import com.open.party.cloud.viewModel.ArticleViewModel
import kotlinx.android.synthetic.main.huan_jie_gong_zuo_main.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class QiYiJiangHuaMainActivity : AppEventBusTitleBaseActivity() {

    private var viewModel: ArticleViewModel? = null

    override fun getLayoutResId(): Int = R.layout.huan_jie_gong_zuo_main

//    private fun initViewListData(type: String) {
//        val pageReq = PageReq<ArticleVo>()
//        pageReq.pageNo = 1
//        pageReq.pageSize = 6
//        val vo = ArticleVo()
//        vo.childrenType = DictionaryBean(type, "")
//        pageReq.data = vo
//
//        viewModel!!.getArticleListData(pageReq, type)
//    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun receiveStudentEventBus(result: ResultInfo<AppPageData<ArticleEntity>>) {
//
//        if ("QHJS_LBXW" == result.eventType) {
//            when (result.code) {
//                ResultInfo.SUCCESS -> {
//                    if (result.data != null && result.data.records != null) {
//                        initBannerView(result.data.records)
//                    } else {
//                    }
//                }
//                else -> {
//                    XUtils.toast().show(result.msg)
//                }
//            }
//        } else {
//            return
//        }
    }

//    /**
//     * 轮播图
//     */
//    private fun initBannerView(records: MutableList<ArticleEntity>) {
//        val titles: ArrayList<String> = java.util.ArrayList()
//        val bannerList: ArrayList<SlideshowListBean> = ArrayList()
//        for (item in records) {
//            titles.add(item.theme)
//            bannerList.add(SlideshowListBean(item.id, item.imgUrl))
//        }
//
//        bannerView!!.setBannerTitles(titles)
//            .setAutoPlay(true)
//            .setPages(bannerList, CustomViewHolder())
//            .setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE)
//            .setBannerAnimation(Transformer.Default)
//            .setDelayTime(3000)
//            .start()
//
//        bannerView!!.setOnBannerClickListener { _, position ->
//            val id = bannerList[position].id
//            if (TextUtils.isEmpty(id)) {
//                return@setOnBannerClickListener
//            }
//            XUtils.intent().openActivity(this, ArticleDetailsActivity::class.java)
//                .putStringExtra("id", bannerList[position].id)
//                .start()
//        }
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setViewTitle("学习七一讲话精神")
        viewModel = ArticleViewModel()
        // 轮播图
//        initViewListData("QHJS_LBXW")

        initTabView()
    }

    private fun initTabView() {
        val titleList = ArrayList<String>()
        val typeList = ArrayList<String>()

        titleList.add("“七一”讲话精神")
        titleList.add("热议反响")

        typeList.add("QYJH_JHJS")
        typeList.add("QYJH_RYFX")

        initTab(
            titleList,
            getFragments(typeList)
        )
    }

    private fun getFragments(typeList: java.util.ArrayList<String>): List<Fragment> {
        val fragments: ArrayList<Fragment> = ArrayList()

        for (type in typeList) {
            val fragment = ArticleListFragment()
            val bundle = Bundle()
            bundle.putString("type", type)
            fragment.arguments = bundle
            fragments.add(fragment)
        }

        return fragments
    }

    private fun initTab(titles: ArrayList<String>, fragments: List<Fragment>) {

        val fragmentPagerAdapter = object : FragmentPagerAdapter(supportFragmentManager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int {
                return fragments.size
            }

            override fun getItem(arg0: Int): Fragment {
                return fragments[arg0]
            }
        }

        viewPager.offscreenPageLimit = titles.size - 1
        viewPager.adapter = fragmentPagerAdapter
        // pager.addOnPageChangeListener(this);

        scrollTab.setTitles(titles, 0) //
//        scrollTab.setNumber(1, "9", View.VISIBLE)//设置数字红点
        scrollTab.setViewPager(viewPager)
        scrollTab.setOnTabListener { index, _ ->
            viewPager.setCurrentItem(index, true)
        }
    }
}